<?php
/**
 * User: hjd-yao
 * Date: 2018/11/13
 * Time: 13:50
 */

namespace App\Http\Controllers\Backend;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Manage\LogsAdminModel;
use App\Models\Rule\BonusRuleModel;
use App\Models\Shop\ShopRuleModel;
use App\Models\System\ChangeTypeModel;
use App\Models\System\ConfigModel;
use App\Models\System\CurrencyModel;
use App\Models\System\RuleModel;
use Carbon\Carbon;
use DC\BonusCenter\Services\ShoppingBonus;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ConfigController extends BackendController
{
    public function __construct()
    {
        view()->share('config_group',config('app.conf_group_list'));
        view()->share('config_type',config('app.conf_type_list'));
    }

    public function gongzi(){
        $bonusapi=new ShoppingBonus();
        $bonusapi->gongzi();
        return SelfResponse::success('操作成功');
    }
    public function futouCheak(){
        $bonusapi=new ShoppingBonus();
        $bonusapi->futouCheak();
        return SelfResponse::success('操作成功');
    }

    /**
     * @title 网站配置
     * @param int $group
     * @return View
     */
    public function showConfig($group=1)
    {
        $list = ConfigModel::where('group',$group)->where('status',1)->get()->toArray();
        foreach ($list as &$item){
            $item['extra'] = ConfigModel::init()->extraFormat($item['extra']);
        }

        return view('backend::config.show_config',[
            'page_title'=>'站点配置',
            'configs'=>$list,
            'current_group'=>$group
        ]);
    }

    /**
     * @title 修改网站配置
     * @param Request $request
     * @param $group
     * @return SelfResponse|JsonResponse|RedirectResponse
     */
    public function updateValue(Request $request,$group)
    {
        $sys_data = ConfigModel::where('group',$group)->select('id','name','value','type')->get()->toArray();
        if(!$sys_data){
            return SelfResponse::error('修改的数据不存在',['b_Config_showConfig',['group'=>1]]);
        }
        $sys_data = array_column($sys_data,null,'name');
        $post_data = $request->post();
        foreach ($sys_data as $key=>$val){
            if(!isset($post_data[$key])&&$val['type'] != 8){
                continue;
            }
            if($val['type'] == 8){
                $val['value'] = str2arr($val['value']);
            }
            if(isset($post_data[$key])&&$val['value']!=$post_data[$key]){
                if (is_array($post_data[$key])){
                    $post_data[$key] = arr2str($post_data[$key]);
                }
                ConfigModel::where('name','=',$key)->update(['value'=>$post_data[$key]]);
            }elseif(!isset($post_data[$key])&&$val['type'] == 8){
                ConfigModel::where('name','=',$key)->update(['value'=>'']);
            }
        }
        ConfigModel::init()->clearConfigCache();
        return SelfResponse::success('修改成功',['b_Config_showConfig',['group'=>$group]]);
    }

    /**
     * @title 网站设置
     * @return View
     */
    public function websiteSet()
    {
        $currencies = CurrencyModel::where('status',1)->get();//系统预设币种
        $changes = ChangeTypeModel::where('status',1)->get(); //系统变更类型
        return view('backend::config.website_set',[
            'page_title'=>'网站设置',
            'currencies'=>$currencies,
            'changes'=>$changes
        ]);
    }

    /**
     * @title 网站规则
     * @param Request $request
     * @return SelfResponse||\Illuminate\View\View
     */
    public function rules(Request $request)
    {
        if($request->isMethod('post')){

            $data = $request->except('_token');

//            $data['bill_money'] = json_encode($data['bill_money']);

//            $data['global_partnership_condition1'] = json_encode($data['global_partnership_condition1']);

//            $data['global_partnership_condition2'] = json_encode($data['global_partnership_condition2']);

//            $data['global_partnership_award'] = json_encode($data['global_partnership_award']);

//            $data['management_condition'] = json_encode($data['management_condition']);

//            $data['management_award'] = json_encode($data['management_award']);

//            $data['flat_level_award'] = json_encode($data['flat_level_award']);

            $data['tf_self_tax'] = json_encode($data['tf_self_tax']);

            $data['wd_time'] = json_encode($data['wd_time']);

            if(!RuleModel::updateOrCreate(['id'=>1],['id'=>1]+$data)){
                return SelfResponse::error('规则更新失败!');
            }
            Cache::forget('sys_rule');
            return SelfResponse::success('规则更新成功');
        }else{
            $bonus_rule_condition1 =  format_rule(json_decode(get_rule('global_partnership_condition1'),true));



            $rules = RuleModel::findOrNew(1);

            $rules->bill_money = json_decode($rules->bill_money);

            $rules->global_partnership_condition1 = json_decode($rules->global_partnership_condition1);

            $rules->global_partnership_condition2 = json_decode($rules->global_partnership_condition2);

            $rules->global_partnership_award = json_decode($rules->global_partnership_award);

            $rules->management_condition = json_decode($rules->management_condition);

            $rules->management_award = json_decode($rules->management_award);

            $rules->flat_level_award = json_decode($rules->flat_level_award);

            $rules->tf_self_tax = json_decode($rules->tf_self_tax);

            $rules->wd_time = json_decode($rules->wd_time);

            return view('backend::config.rules',[
                'page_title'=>'规则设置',
                'rule'=>$rules
            ]);
        }
    }


    /**
     * @title 奖金规则
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function bonusRule(Request $request)
    {
        $rule = BonusRuleModel::get();
        if($request->isMethod('post')){
            $data = $request->post();
            foreach ($rule as $item){
                $value = $data[$item->name]??0;
                if($value){
                    $item->update($value);
                }
            }
            Cache::forget('bonusRule');
            return SelfResponse::success('修改成功');
        }else{
            $rules = BonusRuleModel::get();
            return $this->render('config.bonus_rule',[
                'page_title'=>'结算规则',
                'rules'=> $rules
            ]);
        }
    }

    /**
     * @title 奖金开关
     * @param $rule_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function switchRule($rule_id)
    {
        $info = BonusRuleModel::find($rule_id);
        if(!$info){
            return SelfResponse::error('修改数据不存在');
        }
        $status = abs($info->status-1);
        $info->status = $status>1?1:$status;
        $remark = ($info->status?'开启':'关闭').$info->title;

        if($info->save()){
            LogsAdminModel::init()->setOptTitle('奖金开关')
                ->setRemark($remark)
                ->setOptData([
                    'id'=>$info->id,
                    'name'=>$info->name,
                    'title'=>$info->title,
                    'status'=>$info->status
                ])->recording();
            return SelfResponse::success('操作成功');
        }
        return SelfResponse::error('操作失败');
    }

}